package cj.web.yc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cj.web.uc.dao.MemberDAO;
import cj.web.yc.dao.PointConfigDAO;
import cj.web.yc.dao.VideoDAO;
import cj.web.yc.utils.Constants;

@Service
public class VideoService {
	
	private static final String VIDEO_END ="VIDEO_END";//视频结束——加积分
	private static final String VIDEO_BEGIN ="VIDEO_BEGIN";//视频开始——扣积分
	
	@Autowired
    private VideoDAO videoDAO;
	@Autowired
    private PointConfigDAO pointConfigDAO;
	@Autowired
    private MemberDAO memberDAO;
	
	public List<Map> getVideoDetail(int videoId){
		return videoDAO.queryVideoDetail(videoId);
	}

	/**
	 * 查询
	 * @param video
	 * @return videolis;size
	 */
	public Map query(String categoryIds,int start,int length){
		Map rep = new HashMap();
		List<Map> videolis = videoDAO.queryVideolis(categoryIds,start,length);
		int size = videoDAO.queryVideoSize(categoryIds); 
		rep.put("videolis", videolis);
		rep.put("size", size);
		return rep;
    }
	
	/**
	 * 查询
	 * @param video
	 * @return videolis;size
	 */
	public Map queryVideoDetail(int videoId){
		Map rep = new HashMap();
		List<Map> videolis = videoDAO.queryVideoDetail(videoId);
		
		if(videolis!=null && videolis.size()>0){
			rep.put("videoDetail", videolis.get(0));
			List<Map> videoDetailOtherlis = videoDAO.queryVideolis(videolis.get(0).get("categoryId").toString());
			rep.put("videoDetailOtherlis", videoDetailOtherlis);
		}else{
			rep.put("videoDetail", "");
			rep.put("videoDetailOtherlis", "");
		}
		
		return rep;
    }
	
	/**
	 * 视频播放
	 * @param videoId
	 * @param memberId
	 * @return
	 */
	public Map videoStart(int videoId,int memberId){
		Map rep = new HashMap();
		rep.put("returnFlag", Constants.returnObj.returnFlag_true);
		//视频详情
		List<Map> videolis = videoDAO.queryVideoDetail(videoId);
		List<Map> memberlis = memberDAO.getMemberById(memberId);
		
		if(memberlis!=null && memberlis.size()>0){
			rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
			rep.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		if(videolis!=null && videolis.size()>0){
			//视频类目ID
			int categoryId = Integer.parseInt(videolis.get(0).get("categoryId").toString());
			int point = Integer.parseInt(memberlis.get(0).get("point").toString());
			//视频积分配置视频播放
			List<Map> replis = pointConfigDAO.queryByCode(VIDEO_BEGIN);
			
			if(replis!=null && replis.size()>0){
				//积分配置类目组
				String[] vals = replis.get(0).get("categoryId").toString().split(",");
				//积分
				Integer pointNum = Integer.parseInt(replis.get(0).get("pointNum").toString());
				
				if(point>=pointNum){
					for(int i=0;i<replis.size();i++){
						if(categoryId == Integer.parseInt(vals[i])){
							memberDAO.updateMemberPoint(memberId, pointNum);
						}
					}
				}else{
					rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
					rep.put("returnCode", Constants.returnObj.returnCode.code_5);
				}
			}
			
		}else{
			rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
			rep.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		return rep;
	}
	
	
	/**
	 * 视频结束
	 * @param videoId
	 * @param memberId
	 * @return
	 */
	public Map videoEnd(int videoId,int memberId){
		Map rep = new HashMap();
		rep.put("returnFlag", Constants.returnObj.returnFlag_true);
		//视频详情
		List<Map> videolis = videoDAO.queryVideoDetail(videoId);
		List<Map> memberlis = memberDAO.getMemberById(memberId);
		
		if(memberlis!=null && memberlis.size()>0){
			rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
			rep.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		if(videolis!=null && videolis.size()>0){
			//视频类目ID
			int categoryId = Integer.parseInt(videolis.get(0).get("categoryId").toString());
			//视频积分配置视频播放
			List<Map> replis = pointConfigDAO.queryByCode(VIDEO_END);
			
			if(replis!=null && replis.size()>0){
				//积分配置类目组
				String[] vals = replis.get(0).get("categoryId").toString().split(",");
				//积分
				Integer pointNum = Integer.parseInt(replis.get(0).get("pointNum").toString());
				
				for(int i=0;i<replis.size();i++){
					if(categoryId == Integer.parseInt(vals[i])){
						memberDAO.updateMemberPoint(memberId, pointNum);
					}
				}
			}
			
		}else{
			rep.put("returnFlag", Constants.returnObj.returnFlag_flase);
			rep.put("returnCode", Constants.returnObj.returnCode.code_2);
		}
		
		return rep;
	}
}